package com.heima.behavior.service.impl;

import com.heima.behavior.service.BehaviorService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.behavior.dtos.LikeBehaviorDto;
import com.heima.model.behavior.dtos.ReadBehaviorDto;
import com.heima.model.behavior.dtos.UnlikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.thread.ApThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class BehaviorServiceImpl implements BehaviorService {
        @Autowired
        private CacheService cacheService;
    /**
     * 点赞/取消点赞/喜欢/不喜欢的哈希表结构   value1为点赞，value-1为不喜欢
     *      redisKey                         hashKey          value
     *                                    userId_{userId}      1
     *                                    userId_{userId}      1
     * behavior_like_{articleId}          userId_{userId}      -1
     *                                    userId_{userId}      -1
     *                                    userId_{userId}      -1
     * 阅读哈希表结构
     *      redisKey            hashKey              value(自增)
     *                      articleId_{articleId}     1
     *                      articleId_{articleId}     1
     * behavior_read        articleId_{articleId}     1
     *                      articleId_{articleId}     1
     *                      articleId_{articleId}     1
     */
    /**
     * 用户点赞/取消点赞
     * @param dto
     * @return
     */
    @Override
    public ResponseResult likesBehavior(LikeBehaviorDto dto) {
        //参数校验
        if(null==dto.getArticleId()||null==dto.getOperation()||dto.getOperation()>1||dto.getOperation()<0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        String redisKey = BehaviorConstants.Behavior_Like_Redis_Key_prefix + dto.getArticleId();
        String hashKey = BehaviorConstants.Behavior_Like_Hash_Key_prefix + ApThreadLocalUtil.getUser().getId();
        if(dto.getOperation()==0){
            cacheService.hPut(redisKey,hashKey,"1");
        }else{
            cacheService.hDelete(redisKey,hashKey);
        }
        return ResponseResult.okResult(200,"操作成功");
    }

    /**
     * 更新redis中点赞/不喜欢哈希表的数据
     * @param ArticleId
     * @param type
     */
    private void updateRedisLikeData(Long ArticleId , Short type) {

    }

    /**
     * 用户阅读
     * @param dto
     * @return
     */
    @Override
    public ResponseResult readBehavior(ReadBehaviorDto dto) {
        if(null==dto.getArticleId()){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        String redisKey = BehaviorConstants.Behavior_Read_Redis_Key;
        String hashKey = BehaviorConstants.Behavior_Read_Hash_Key_prefix+dto.getArticleId();
        cacheService.hIncrBy(redisKey,hashKey,dto.getCount());
        return ResponseResult.okResult(200,"操作成功");
    }

    /**
     * 不喜欢/取消不喜欢
     * @param dto
     * @return
     */
    @Override
    public ResponseResult unlikesBehavior(UnlikesBehaviorDto dto) {
        //参数校验
        if(null==dto.getArticleId()||null==dto.getType()||dto.getType()>1||dto.getType()<0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        String redisKey = BehaviorConstants.Behavior_Like_Redis_Key_prefix + dto.getArticleId();
        String hashKey = BehaviorConstants.Behavior_Like_Hash_Key_prefix + ApThreadLocalUtil.getUser().getId();
        if(dto.getType()==0){
            cacheService.hPut(redisKey,hashKey,"-1");
        }else{
            cacheService.hDelete(redisKey,hashKey);
        }
        return ResponseResult.okResult(200,"操作成功");
    }
}
